/**
 * 
 */
package com.netjava.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.netjava.pojo.AucRecord;
import com.netjava.pojo.Notice;
import com.netjava.pojo.Product;
import com.netjava.pojo.User;
import com.netjava.service.IAucRecordService;
import com.netjava.service.INoticeService;
import com.netjava.service.IProductService;

/**
 * @author chenchaohui
 *
 * 夺金网
 * 关注商品
 */
public class NoticeAction extends BaseAction {

	/**
	 * 用户点击关注商品，如果用户已经关注商品没有必要再关注，否则在数据库加入一条关注记录
	 * 
	 */
	 public ActionForward notice(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		 response.setContentType("text/html;charset=UTF-8"); 
		 response.setHeader("Cache-Control", "no-cache"); 

		 PrintWriter out=response.getWriter();
		 int productId=Integer.parseInt(request.getParameter("id"));
		 
		 //通过产品id找到相应的产品
		 IProductService productService=(IProductService)getBean("productService");
		 Product product = productService.getProById(productId);
		 
		 //得到用户对象
		 User user=(User)request.getSession().getAttribute("loginUser");
		 
		 //检查用户是否已经关注该商品
		 INoticeService noticeService=(INoticeService)getBean("noticeService");
		 
		 Notice notice=new Notice();
		 notice.setProduct(product);
		 notice.setUser(user);
		 try{
			 noticeService.addNotice(notice);
			 out.print("成功添加关注商品。");
			 
		 }catch(Exception e){
			 out.print("你已经关注了该商品！你可以关注其他的商品。");
		 }
		 out.flush();
		 out.close();
		 
				return null;
	}
	 

		/**
		 * 用户后台管理，查询该用户所有关注的商品
		 * 
		 */
	 public ActionForward findProByUser(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response) throws IOException {
		 
		//得到用户对象
		 User user=(User)request.getSession().getAttribute("loginUser");
		 
		 //得到该用户所有关注的商品
		 INoticeService noticeService=(INoticeService)getBean("noticeService");
		 List<Notice> notices = noticeService.getByUserId(user);
		 
		 IAucRecordService aucRecordService=(IAucRecordService)getBean("aucRecordService");
		
		 List<AucRecord> records=new ArrayList<AucRecord>();
		 List<Product> products=new ArrayList<Product>();
		 
		 for(int i=0;i<notices.size();i++){
			 Notice notice=notices.get(i);
			 Product pro = notice.getProduct();
			//得到商品的所有竞拍记录，找出商品的及时信息
			 List<AucRecord> aucRecords = aucRecordService.getByProId(pro);
			 
			 //0是待拍卖的商品
			 if(pro.getState()==0){
				 products.add(pro);
				 request.setAttribute("msg", "还没有开始拍卖。");
			 }
			 
			
			 else {
				
				 if(pro.getState()==1){
					 
					 //1是正在拍卖的商品
					 if(aucRecords.size()<=0){
						 products.add(pro);
						 request.setAttribute("msg", "没有拍卖记录。");
					 }
					 else{
						 records.add(aucRecords.get(0));
					 }
					 
				 }
				 else{
					 //2是结束拍卖的商品
					 records.add(aucRecords.get(aucRecords.size()-1));
				 }
				
			 }
			
			 
		 }
		 if(records.size()<=0&&products.size()<=0){
			 request.setAttribute("msg", "没有记录。");
		 }
		
		 request.setAttribute("records", records);
		 request.setAttribute("products", products);
		 
		 
		 ActionForward forward=mapping.getInputForward();
		 forward=mapping.findForward("allNotes");
		 return forward;
		
	 }

}
